/**
 * 公司资讯
 */
const NewsInfo = require("../model/companyNews");
const { validationResult } = require("express-validator");
const cleanHtml = require("../modules/sanitizeHtml");

module.exports = {
  // 获取新闻资讯列表
  async getCompanyNewsList(req, res) {
    const { query, body, params } = req;
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array({ onlyFirstError: true }) });
    }
    const { page, limit } = req.query;
    const offset = (page - 1) * limit;
    try {
      const news = await NewsInfo.findAndCountAll({
        offset,
        limit,
        order: [["create_time", "DESC"]],
      });
      res.json({
        code: 1,
        data: news.rows,
        total: news.count,
      });
    } catch (error) {
      console.error("服务器错误", error);
      res.json({
        code: 500,
        message: "服务器错误",
      });
    }
  },
  // 获取产品详情
  async getCompanyNewsDetail(req, res) {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array({ onlyFirstError: true }) });
    }
    const { news_id } = req.query;
    // const sql = `SELECT news_id,title,cover,content,view_count,create_time,update_time FROM news_info WHERE news_id = ?`;
    try {
      // const [result] = await db.execute(sql, [news_id]);
      const result = await NewsInfo.findOne({
        where: {
          news_id,
        },
      });
      if (result === null) {
        return res.json({ code: -1, message: "查无数据" });
      }
      res.json({ code: 1, data: result });
    } catch (error) {
      res.json({
        code: 500,
        message: "服务器错误",
      });
    }
  },
  // 新增新闻资讯
  async editCompanyNews(req, res) {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array({ onlyFirstError: true }) });
    }
    const { news_id, title, content, cover } = req.body;
    const cleanContent = cleanHtml(content);
    try {
      // 判断是否是更新操作
      if (req.body.news_id) {
        const result = await NewsInfo.update(
          { title, content: cleanContent, cover },
          {
            where: { news_id },
          }
        );
        if (result[0] === 0) {
          return res.json({ code: -1, message: "查无数据" });
        }
      } else {
        const result = await NewsInfo.create({ title, content: cleanContent, cover });
        if (result === null) {
          return res.json({ code: -1, message: "新增失败" });
        }
      }
      res.json({ code: 1, message: `${news_id ? "更新" : "新增"}成功` });
    } catch (error) {
      console.error(error);
      res.json({
        code: 500,
        message: "服务器错误",
      });
    }
  },
  // 删除新闻资讯
  async deleteCompanyNews(req, res) {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array({ onlyFirstError: true }) });
    }
    const { news_id } = req.query;
    try {
      // 查询是否存在
      const result = await NewsInfo.findOne({
        where: { news_id },
      });
      if (result === null) {
        return res.json({ code: -1, message: "查无数据" });
      }
      // 逻辑删除
      await NewsInfo.destroy({
        where: { news_id },
      });
      res.json({ code: 1, message: "删除成功" });
    } catch (error) {
      console.error(error);
      res.json({
        code: 500,
        message: "服务器错误",
      });
    }
  },
};
